package com.example.demo.controller;

import com.example.demo.entity.*;
import com.example.demo.mapper.*;
import org.apache.ibatis.annotations.Delete;
import org.springframework.web.bind.annotation.*;
import org.springframework.beans.factory.annotation.Autowired;

import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

@RestController
public class EquipmentController {

    @Autowired
    EquipmentMapper equipmentMapper;

    @RequestMapping("/getEquOfProject")
    public List<Equipment> getEquOfProject(int pjid)
    {
        List<Equipment> equipments = new ArrayList<>();
        List<Integer> eids = equipmentMapper.findEquIdOfProject(pjid);

        if(eids != null)
        for (int eid : eids)
        {
            Equipment equipment = equipmentMapper.findEquById(eid);
            equipments.add(equipment);
        }

        return equipments;
    }

    @DeleteMapping("/deleteEquOfProject")
    public int deleteEquOfProject(int eid)
    {
        return equipmentMapper.deleteEquOfProject(eid);
    }

    @DeleteMapping("/deleteEquListOfProject")
    public int deleteEquListOfProject(@RequestBody List<Integer> eIds)
    {

        //String eids = eIds.stream().map(String::valueOf).collect(Collectors.joining(","));

        return equipmentMapper.deleteEquListOfProject(eIds);
    }

    @PutMapping("/addEquOfProject")
    public int addEquOfProject(int pjId,String eName,String eUnit)
    {
        Equipment equipment = new Equipment();
        Equconnect equconnect = new Equconnect();

        equipment.seteName(eName);
        equipment.seteUnit(eUnit);

        equipmentMapper.addEquOfProject(equipment);

        equconnect.setPjId(pjId);
        equconnect.seteId(equipment.geteId());

        return equipmentMapper.addEquConnect(equconnect);
    }

}
